package com.atguigu.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.entities.PayDTO;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.resp.ReturnCodeEnum;
import com.atguigu.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {
    @Resource
    private  PayService payService;

    //添加支付信息
    @PostMapping(value = "/pay/add" )
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay(@RequestBody Pay pay){
        log.info ( "addPay, pay = {}", pay.toString ( ) );
        int i = payService.addPay(pay);
        return ResultData.success ("成功插入记录,成功条数 : "+i);
    }
    @DeleteMapping(value = "/pay/delete/{id}" )
    @Operation(summary = "删除",description = "删除支付流水方法")
    public ResultData<String> deletePay(@PathVariable("id") Integer id){
        log.info ( "deletePay, id = {}", id );
        int i = payService.deletePay(id);
        return ResultData.success ( "成功删除记录,成功条数 : "+i);
    }
    @PutMapping(value = "/pay/update" )
    @Operation(summary = "修改",description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO, pay);
        log.info ( "updatePay, pay = {}", pay.toString ( ) );
        int i = payService.updatePay(pay);
        return ResultData.success ( "成功更新记录,成功条数 : "+i);
    }
    @GetMapping(value = "/pay/getById/{id}" )
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData<Pay> getPayById(@PathVariable("id") Integer id){
        if(id == -4) throw new RuntimeException("id不能为负数");
        log.info ( "getPayById, id = {}", id );
        return ResultData.success (payService.getPayById(id));
    }
    @GetMapping(value = "/pay/getList" )
    @Operation(summary = "查询所有支付流水",description = "查询所有支付流水方法")
    public ResultData<List<Pay>> getPayList(){
        log.info("getPayList");
        return ResultData.success (payService.getPayList());
    }

    @RequestMapping(value = "/pay/error",method = RequestMethod.GET)
    public ResultData<Integer> getPayError()
    {
        Integer i = Integer.valueOf(200);
        try
        {
            System.out.println("--------come here");
            int data = 10/0;
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.fail( ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }
        return ResultData.success(i);
    }
}
